package com.woniu.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.PageResult;
import com.woniu.Result;
import com.woniu.shop.pojo.ServiceItem;
import com.woniu.shop.mapper.ServiceItemMapper;
import com.woniu.shop.pojo.vo.ServiceItemVo;
import com.woniu.shop.service.IServiceItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaohuang
 * @since 2022-11-16
 */
@Service
public class ServiceItemServiceImpl extends ServiceImpl<ServiceItemMapper, ServiceItem> implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;

    /**
     * 修改
     * 修改后默认改为下架，要经过确认后才能上架
     * @param serviceItem
     * @return
     */
    @Override
    public Result<String> updateServiceItem(ServiceItem serviceItem) {
        serviceItem.setIsShelves(0);
        int i = serviceItemMapper.updateById(serviceItem);
        return i > 0 ? Result.ok():Result.fail();
    }

    @Override
    public Result<PageResult> pageSearch(ServiceItemVo serviceItemVo, Integer currentPage, Integer pageSize) {
        QueryWrapper<ServiceItem> wrapper = new QueryWrapper<>();
        if (StringUtil.isNullOrEmpty(serviceItemVo.toString())){
            if (serviceItemVo.getIsShelves() != null ){
                wrapper.eq("is_shelves", serviceItemVo.getIsShelves());
            }
            if (serviceItemVo.getProjectName() != null){
                wrapper.like("project_name",serviceItemVo.getProjectName());
            }

            if (serviceItemVo.getServiceTypeId() != null){
                wrapper.like("service_type_time",serviceItemVo.getServiceTypeId());
            }
        }

        IPage<ServiceItem> iPage = new Page<>(currentPage,pageSize);
        IPage<ServiceItem> serviceItemIPage = serviceItemMapper.selectPage(iPage, wrapper);
        PageResult pageResult = new PageResult(serviceItemIPage.getTotal(), serviceItemIPage.getRecords());

        return Result.ok(pageResult);
    }

    @Override
    public Result<String> audit(ServiceItem serviceItem) {
        //服务未发布，不能上架
        if (serviceItem.getIsRelease() == 0){
            return Result.fail("服务未发布，不能上架");
        } else {
            int i = serviceItemMapper.updateById(serviceItem);
            return Result.ok();
        }

    }
}
